﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using QuickGame.Model;
using System.Collections;

namespace QuickGame.View
{
    public partial class QLCauHoi : System.Web.UI.Page
    {
        private ModelContext modelContext;
        private long idcauhoi;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.modelContext = new ModelContext();
            if (Session["idnguoidung"] == null)
            {
                Response.Redirect("/Account/Login.aspx");
            }
            else
            {
                long idnguoidung = (long)Session["idnguoidung"];
                var nguoidung = NguoiDungHs.QueryEntity(modelContext, idnguoidung);
                if (nguoidung.IDQuyen ==1)
                {
                    this.LoadDL();
                }
                else
                {
                    Response.Redirect("/Account/Login.aspx");
                }
            }
            
        }
        public void LoadDL()
        {
            this.modelContext = new ModelContext();
            var ds = CauHoiHs.QueryEntities(modelContext).Select(k => new CauHoiView
            {
                ID = k.ID,
                Ma = k.Ma,
                Ten = k.Ten,
                Loai = k.IDLoai,
                CapDo = k.IDCapDo
            });
            grvCauHoi.DataSource = ds;
            grvCauHoi.DataBind();
        }
        #region Câu hỏi
        protected void ASPxGridView2_BeforePerformDataSelect(object sender, EventArgs e)
        {

        }
        protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            string ten = (string)e.NewValues["Ten"];
            long? capdo = (long?)e.NewValues["CapDo"];
            if (ten ==null || String.IsNullOrEmpty(ten.Trim()) ||capdo ==null)
            {
                e.Cancel = true;
            }
            else
            {
                long id = (long)e.Keys[0];
                CauHoiView view = new CauHoiView();
                view.ID = id;
                view.Ma = (string)e.NewValues["Ma"];
                view.Ten = ten;
                view.Loai = (long?)e.NewValues["Loai"];
                view.CapDo = capdo;
                CauHoiHs.Update(modelContext, view);
                this.LoadDL();
                e.Cancel = true;
                grvCauHoi.CancelEdit();
            }
            
        }

        protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {

        }

        protected void ASPxGridView1_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {
            if (e.Column.FieldName == "Loai")
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                this.modelContext = new ModelContext();
                var loai = LoaiCauHoiHs.QueryEntities(modelContext).Select(k => new
                {
                    Loai = k.ID,
                    Ma = k.Ma,
                    Ten = k.Ten,
                });
                combo.DataSource = loai;
                combo.DataBind();
            }
            if (e.Column.FieldName == "CapDo")
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                this.modelContext = new ModelContext();
                var capdo = CapDoHs.QueryEntities(modelContext).Select(k => new
                {
                    CapDo = k.ID,
                    Ma = k.Ma,
                    Ten = k.Ten,
                });
                combo.DataSource = capdo;
                combo.DataBind();
            }

        }
        protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            ASPxGridView grvcauhoi = sender as ASPxGridView;
            string ten = (string)e.NewValues["Ten"];
            long? capdo = (long?)e.NewValues["CapDo"];
            if (ten == null || String.IsNullOrEmpty(ten.Trim()) || capdo == null)
            {
                e.Cancel = true;
            }
            else
            {
                var cauhoi = CauHoiHs.Insert(modelContext);
                cauhoi.Ma = (string)e.NewValues["Ma"];
                cauhoi.Ten = (string)e.NewValues["Ten"];
                cauhoi.IDLoai = (long?)e.NewValues["Loai"];
                cauhoi.IDCapDo = (long?)e.NewValues["CapDo"];
                if (this.modelContext.SaveChanges() > 0)
                {
                    this.LoadDL();
                }
                e.Cancel = true;
                grvCauHoi.CancelEdit();
            }
            
        }
        protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            ASPxGridView grid = sender as ASPxGridView;
            var id = e.Keys[0];
            CauHoi cauhoi = CauHoiHs.QueryEntity(modelContext, (long)id);
            if (cauhoi.ChiTietChois.Count()>0)
            {
                e.Cancel = true;
            }
            else
            {
                CauHoiHs.Delete(modelContext, (long)id);
                if (this.modelContext.SaveChanges() > 0)
                {
                    this.LoadDL();
                }
                e.Cancel = true;
            }
        }
        #endregion

        #region Câu trả lời
        protected void GrvCauTraLoi_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            ASPxGridView grid = sender as ASPxGridView;
            idcauhoi = (long)grid.GetMasterRowKeyValue();
            var id = e.Keys[0];
            CauTraLoi cautraloi = CauTraLoiHs.QueryEntity(modelContext, (long)id);
            if (cautraloi.ChiTietChois.Count()>0)
            {
                e.Cancel = true;
            }
            else
            {
                CauTraLoiHs.Delete(modelContext, (long)id);
                if (this.modelContext.SaveChanges() > 0)
                {
                    this.LoadCauTraLoi(grid, idcauhoi);
                }
                e.Cancel = true;
            }
            
        }

        protected void GrvCauTraLoi_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            ASPxGridView grvtraloi = sender as ASPxGridView;
            idcauhoi = (long)grvtraloi.GetMasterRowKeyValue();
            long id = (long)e.Keys[0];
            var cauhoi = CauHoiHs.QueryEntity(modelContext, idcauhoi);
            string ten = (string)e.NewValues["Ten"];
            bool? trangthai = e.NewValues["TrangThai"] != null ? (bool)e.NewValues["TrangThai"] : false;
            if (ten == null || String.IsNullOrEmpty(ten.Trim()))
            {
                e.Cancel = true;
            }
            else
            {
                int a = cauhoi.CauTraLois.Where(k => k.TrangThai == true && k.ID!= id).Count();
                if ((trangthai == true && a <= 0) || (trangthai == false))
                {
                    CauTraLoiView view = new CauTraLoiView();
                    view.ID = id;
                    view.Ma = (string)e.NewValues["Ma"];
                    view.Ten = (string)e.NewValues["Ten"];
                    view.TrangThai = (bool)e.NewValues["TrangThai"];
                    CauTraLoiHs.Update(modelContext, view);
                    this.LoadCauTraLoi(grvtraloi, idcauhoi);
                    e.Cancel = true;
                    grvtraloi.CancelEdit();
                }
                else if (trangthai == true && a > 0)
                {
                    e.Cancel = true;
                }

            }
            
        }

        protected void ASPxGridView1_DetailRowExpandedChanged(object sender, ASPxGridViewDetailRowEventArgs e)
        {


        }

        protected void GrvCauTraLoi_Load(object sender, EventArgs e)
        {
            ASPxGridView grvtraloi = sender as ASPxGridView;
            idcauhoi = (long)grvtraloi.GetMasterRowKeyValue();
            this.LoadCauTraLoi(grvtraloi, idcauhoi);
        }

        protected void GrvCauTraLoi_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            ASPxGridView grvtraloi = sender as ASPxGridView;
            idcauhoi = (long)grvtraloi.GetMasterRowKeyValue();
            var cauhoi = CauHoiHs.QueryEntity(modelContext, idcauhoi);
            if (cauhoi.CauTraLois.Count()<4)
            {
                string ten = (string)e.NewValues["Ten"];
                bool? trangthai = e.NewValues["TrangThai"] != null ? (bool)e.NewValues["TrangThai"] : false;
                if (ten == null || String.IsNullOrEmpty(ten.Trim()))
                {
                    e.Cancel = true;
                }
                else
                {
                    int a = cauhoi.CauTraLois.Where(k => k.TrangThai == true).Count();
                    if ((trangthai == true && a <= 0) || (trangthai == false))
                    {
                        var cautraloi = CauTraLoiHs.Insert(modelContext);
                        cautraloi.IDCauHoi = idcauhoi;
                        cautraloi.Ma = (string)e.NewValues["Ma"];
                        cautraloi.Ten = (string)e.NewValues["Ten"];
                        cautraloi.TrangThai = trangthai;
                        if (this.modelContext.SaveChanges() > 0)
                        {
                            this.LoadCauTraLoi(grvtraloi, idcauhoi);
                        }
                        e.Cancel = true;
                        grvtraloi.CancelEdit();
                    }
                    else if (trangthai ==true && a >0)
                    {
                        e.Cancel = true;
                    }
                    
                }
            }
            else
            {
                e.Cancel = true;
            }
            
        }
        public void LoadCauTraLoi(ASPxGridView grview, long idcauhoi)
        {
            var cautraloi = CauTraLoiHs.QueryEntities(modelContext)
                .Where(k => k.IDCauHoi == idcauhoi)
                .Select(k => new CauTraLoiView
                {
                    ID = k.ID,
                    IDCauHoi = k.IDCauHoi,
                    Ma = k.Ma,
                    Ten = k.Ten,
                    TrangThai = k.TrangThai
                });
            grview.DataSource = cautraloi;
            grview.DataBind();
        }
        #endregion

        protected void grvCauHoi_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
        {
            
        }

        
         
    }
}